package com.zhoug.zmui3;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.zhoug.common3.utils.LogUtils;

import androidx.annotation.Nullable;

/**
 * 圆形指示器, 小圆点[ o o o o o]
 *
 * @Author HK-LJJ
 * @Date 2019/12/11
 * @Description
 */
public class ZMUIRoundIndicatorView extends View {
    private static final String TAG = ">>>ZMUIIndicatorView";
    private static final boolean DEBUG = false;

    private boolean selectFill;//选中时是否填充圆
    private boolean normalFill;//非选中时是否填充圆
    private int colorSelected;//选中时的颜色
    private int colorNormal;//非选中时的颜色

    private int radius;//圆大小
    private int count;//圆的总数
    private int selection;//选择
    private float strokeWidth;
    private int distance;//圆之间的间隔,默认半径
    private int width;
    private int height;
    private Paint mPaint;

    public ZMUIRoundIndicatorView(Context context) {
        this(context, null);
    }

    public ZMUIRoundIndicatorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZMUIRoundIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZMUIRoundIndicatorView, 0, 0);
        colorSelected = a.getColor(R.styleable.ZMUIRoundIndicatorView_zmui_colorSelected, Color.BLACK);
        colorNormal = a.getColor(R.styleable.ZMUIRoundIndicatorView_zmui_colorNormal, Color.GRAY);
        radius = a.getDimensionPixelOffset(R.styleable.ZMUIRoundIndicatorView_zmui_radius, 20);
        count = a.getInt(R.styleable.ZMUIRoundIndicatorView_zmui_count, 3);
        distance = a.getDimensionPixelOffset(R.styleable.ZMUIRoundIndicatorView_zmui_distance, radius);
        selection = a.getInt(R.styleable.ZMUIRoundIndicatorView_zmui_selection, 0);
        selectFill = a.getBoolean(R.styleable.ZMUIRoundIndicatorView_zmui_selectFill, true);
        normalFill = a.getBoolean(R.styleable.ZMUIRoundIndicatorView_zmui_normalFill, true);
        strokeWidth = a.getDimension(R.styleable.ZMUIRoundIndicatorView_zmui_strokeWidth, 3);
        a.recycle();
        initPaint();
        mPaint.setStrokeWidth(strokeWidth);
    }

    private void initPaint() {
        if (mPaint == null) {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Paint.Style.FILL);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(colorNormal);
        float r = radius - strokeWidth / 2;//非填充模式时,半径需要减去画笔宽度的一般否则会画出边界
        float x = radius;
        float y = height >> 1;
        for (int i = 0; i < count; i++) {
            if (i != selection) {
                if (normalFill) {
                    mPaint.setStyle(Paint.Style.FILL);
                    canvas.drawCircle(x, y, radius, mPaint);
                } else {
                    mPaint.setStyle(Paint.Style.STROKE);
                    canvas.drawCircle(x, y, r, mPaint);
                }
            } else {
                mPaint.setColor(colorSelected);
                if (selectFill) {
                    mPaint.setStyle(Paint.Style.FILL);
                    canvas.drawCircle(x, y, radius, mPaint);
                } else {
                    mPaint.setStyle(Paint.Style.STROKE);
                    canvas.drawCircle(x, y, r, mPaint);
                }
                mPaint.setColor(colorNormal);
            }
            x += radius * 2 + distance;
        }

        super.onDraw(canvas);
    }

    public int getColorSelected() {
        return colorSelected;
    }

    public void setColorSelected(int colorSelected) {
        this.colorSelected = colorSelected;
    }

    public int getColorNormal() {
        return colorNormal;
    }

    public void setColorNormal(int colorNormal) {
        this.colorNormal = colorNormal;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
        requestLayout();
    }

    public int getSelection() {
        return selection;
    }

    public void setSelection(int selection) {
        this.selection = selection;
        postInvalidate();
    }

    public int getDistance() {
        return distance;
    }

    public void setDistance(int distance) {
        this.distance = distance;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (DEBUG) {
            LogUtils.d(TAG, "onMeasure:");
        }
        width = radius * 2 * count + distance * (count - 1);
        height = radius * 2;
        int w = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
        int h = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(w, h);
    }

}
